<template> 
  <!-- 提交时间操作 -->
  <el-dialog
    v-model="open2"
    :before-close="cancle2"
    width="30%"
    :title="title2"
  >
    <el-form
      ref="queryForm"
      :inline="true"
      :model="form3"
      label-width="110px"
    >
      <el-form-item
        label="操作时间"
        prop="operationTime"
        :rules="{
          required: true,
          message: '请输入操作时间',
          trigger: 'blur',
        }"
      >
        <el-date-picker
          v-model="form3.operationTime"
          value-format="YYYY-MM-DD"
          type="date"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
    
      <div class="dialog-footer">
        <el-button @click="cancle2">取 消</el-button>
        <el-button type="primary" @click="sumit2">确 定</el-button>
      </div>
    </el-form>
  </el-dialog>
  <!-- 应收费 -->
  <feeName ref="feeRef"></feeName>
  <!-- 应付费 -->
  <payName ref="payRef" :transport-type="props.transportType"></payName>
  <!-- 上传下载文件 -->
  <uploads ref="uploadRef"></uploads>
  <!-- 详情 -->
  <edit ref="editRef"></edit>
  <!-- 配舱 -->
  <LoadingCarbin ref="carbinRef"></LoadingCarbin>
</template>

<script setup lang="ts">
import { ref, 
  defineProps, 
  defineAsyncComponent, 
  defineEmits, 
  getCurrentInstance } from 'vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import { 
  signOut, 
  deletes, 
  domesticInspection, 
  domesticDomestically, 
  foreignInspection, 
  domesticDeclaration,  
  customsClearance, 
  editOrderStatus,
  carryCabinet,
  submitUps,
  submitOverseas,
  saveOrderArrivedTrack } from '@/api/expressManager/flySea'

const feeName = defineAsyncComponent(() => import('./feeName.vue'))
const payName = defineAsyncComponent(() => import('./payName.vue'))
const uploads = defineAsyncComponent(() => import('./uploads.vue'))
const edit = defineAsyncComponent(() => import('./edit.vue'))
const LoadingCarbin = defineAsyncComponent(() => import('./loadingCarbin.vue'))
const props = defineProps({
  statuss: {
    type: Array,
    required: true,
  },
  idss: {
    type: Array,
    required: true,
  },
  loadNos: {
    type: Array,
    required: true,
  },
  transportType: {
    type: Number,
    required: true,
  },
})

const {
  appContext: {
    config: {
      globalProperties: { $validateForm },
    },
  },
} = getCurrentInstance()
const carbinRef = ref(null)
const feeRef = ref(undefined)
const payRef = ref(undefined)
const uploadRef = ref(undefined)
const editRef = ref(undefined)
const emit = defineEmits(['success'])
const open2 = ref(false)
const title2 = ref(null)
const queryForm = ref(undefined)
const form3 = ref({
  operationTime: [],
})

const cancle2 = () => {
  open2.value = false
  queryForm.value.resetFields()
}
const sumit2 = () => {
  const ids = JSON.parse(JSON.stringify(props.idss))
  const loadNo = JSON.parse(JSON.stringify(props.loadNos))

  $validateForm(queryForm.value, () => {
    const data = {
      operationTime: form3.value.operationTime,
      ladingNoList: loadNo,
      idList: ids,
    }
    sentRequest(data).then(res => {
      if (res.code == 200) {
        open2.value = false
        ElMessage.success('操作成功')
        queryForm.value.resetFields()
        emit('success')
      } else {
        ElMessage.error(res.msg)
      }
    })  
  })
}

const sentRequest = async (params) => {
  let Res
  switch (title2.value) {
    case '签出':
      Res = await signOut(params)
      break
    case '国内报关':
      Res = await domesticDeclaration(params)
      break
    case '国内查验':
      Res = await domesticInspection(params)
      break
    case '国内已放行':
      Res = await domesticDomestically(params)
      break
    case '国外清关':
      Res = await customsClearance(params)
      break
    case '国外查验':
      Res = await foreignInspection(params)
      break
    case '国外清关已放行':
      Res = await editOrderStatus(params)
      break
    case '已到港':
      Res = await saveOrderArrivedTrack(params)
      break
    case '已提柜':
      Res = await carryCabinet(params)
      break
    case '提重柜':
      Res = await submitUps(params)
      break
    case '还空柜':
      Res = await submitOverseas(params)
      break
  }
  return Res
}
const handleInspection = () => {
  const status = JSON.parse(JSON.stringify(props.statuss))
  if (status.every(i => i === '5')) {
    open2.value = true
    title2.value = '国内查验'
  } else {
    ElMessage.error('只有签出的才能操作')
  }
}
const handleDeclaration = () => {
  const status = JSON.parse(JSON.stringify(props.statuss))
  if (status.every(i => i === '5')) {
    open2.value = true
    title2.value = '国内报关'
  } else {
    ElMessage.error('只有签出的才能操作')
  }
}
const handleDomestically = () => {
  open2.value = true
  title2.value = '国内已放行'
}
const handleArrived = () => {
  const status = JSON.parse(JSON.stringify(props.statuss))
  if (status.every(i => i === '5')) {
    open2.value = true
    title2.value = '已到港'
  } else {
    ElMessage.error('只有签出的才能操作')
  }
}
const handleForeignInspection = () => {
  const status = JSON.parse(JSON.stringify(props.statuss))
  if (status.every(i => i === '5')) {
    open2.value = true
    title2.value = '国外查验'
  } else {
    ElMessage.error('只有签出的才能操作')
  }
}
const customsClearanceFn = () => {
  const status = JSON.parse(JSON.stringify(props.statuss))
  if (status.every(i => i === '5')) {
    open2.value = true
    title2.value = '国外清关'
  } else {
    ElMessage.error('只有签出的才能操作')
  }
}
const handleCustomsClearance = () => {
  const status = JSON.parse(JSON.stringify(props.statuss))
  if (status[0] == '5') {
    open2.value = true
    title2.value = '国外清关已放行'
  } else {
    ElMessage.error('只有签出的才能操作')
  }
}
const handleCarryCabinet = () => {
  const status = JSON.parse(JSON.stringify(props.statuss))
  if (status.every(i => i === '5')) {
    open2.value = true
    title2.value = '已提柜'
  } else {
    ElMessage.error('只有签出的才能操作')
  }
}
const handleUps = () => {
  const status = JSON.parse(JSON.stringify(props.statuss))
  if (status.every(i => i === '5')) {
    open2.value = true
    title2.value = '提重柜'
  } else {
    ElMessage.error('只有签出的才能操作')
  }
}
const handleOverseas = () => {
  const status = JSON.parse(JSON.stringify(props.statuss))
  if (status.every(i => i === '5')) {
    open2.value = true
    title2.value = '还空柜'
  } else {
    ElMessage.error('只有签出的才能操作')
  }
}

const {
  appContext: {
    config: {
      globalProperties: { $download },
    },
  },
} = getCurrentInstance()
// ---------------------------列表操作按钮--------
const handleCabin = (row) => {
  const { id, status } = row
  if (status == 4) {
    carbinRef.value.title = '配舱信息'
    carbinRef.value.tsNo = 3
    carbinRef.value.openFn({
      id,
    })
  } else {
    ElMessageBox.alert('只已排柜的才能操作!')
  }
}

// 签出
const handleAddTotalOrder = (row) => {
  if (row.status == '4') {
    open2.value = true
    title2.value = '签出'
  } else {
    ElMessage.error('只有已排柜的才能操作')
  }
}
// 删除
const handleDelete = (row) => {
  const { id, status } = row
  if (status == '4') {
    ElMessageBox.confirm('敏感操作, 是否继续?', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(() => {
      deletes(id).then(res => {
        if (res.code == 200) {
          ElMessage.success('删除成功!')
          // mainConfig.value.getList()
          emit('success')
        } else {
          ElMessage.error(res.msg)
        }
      })
    })
  } else {
    ElMessage.error('只有已排柜的才能操作')
  }
}

const handleEdit = (row) => {
  editRef.value.title = '修改'
  editRef.value.idss = row.id
  editRef.value.open = true
  editRef.value.loading = true
  editRef.value.initData(row.id)
}

const tofee = (row: { [key: string]: any }) => {
  feeRef.value.ladingNos = row.ladingNo
  feeRef.value.idss = row.id
  feeRef.value.caseNos = row.caseNo
  feeRef.value.title = '应收费'
  feeRef.value.open = true
  feeRef.value.getList(row.ladingNo)
}
const toPay = (row: { [key: string]: any }) => {
  payRef.value.ladingNos = row.ladingNo
  payRef.value.idss = row.id
  payRef.value.caseNos = row.caseNo
  payRef.value.title = '应付费'
  payRef.value.open = true
  payRef.value.getList(row.orderNo)
}

const handleRouterPush = (row: { [key: string]: any }) => {
  const id = row.id
  editRef.value.title = '详情'
  editRef.value.open = true
  editRef.value.loading = true
  editRef.value.idss = row.id
  editRef.value.orderNos = row.orderNo
  editRef.value.initData(id)
}
// 导出账单
const ExportBillHandler = (row: { [key: string]: any }) => {
  const ladingNo = row.ladingNo
  const customerCode = row.customerCode
  $download('order/airOrderInfo/export/order/info', { ladingNo, customerCode }, `${ customerCode }_${ new Date().getTime() }.xlsx`)
}
// 上传下载文件
const uploadingHandler = (row: { [key: string]: any }) => {
  uploadRef.value.ladingNo = row.ladingNo
  uploadRef.value.open()
}

defineExpose({
  handleInspection,
  handleDeclaration,
  handleDomestically,
  handleArrived,
  handleForeignInspection,
  customsClearanceFn,
  handleCustomsClearance,
  handleCarryCabinet,
  handleUps,
  handleOverseas,
  uploadingHandler,
  handleRouterPush,
  ExportBillHandler,
  toPay,
  handleDelete,
  handleAddTotalOrder,
  handleCabin,
  handleEdit,
  tofee,
})

</script>
